package leetcode;


public class l0023_合并K个升序链表_3_逐一比较 {

    public ListNode mergeKLists(ListNode[] lists) {
        //前置校验
        if (lists == null || lists.length == 0) {
            return null;
        }
        ListNode dummyHead = new ListNode(0);
        ListNode cur = dummyHead;
        while (true) {
            // 最小链表结点所在的索引
            int minIndex = -1;
            for (int i = 0; i < lists.length; i++) {
                if (lists[i] == null) {
                    continue;
                }
                if (minIndex == -1 || lists[i].val < lists[minIndex].val) {
                    minIndex = i;
                }
            }
            //所有的链表结点已经串起来啦
            if (minIndex == -1) {
                break;
            }
            cur.next = lists[minIndex];
            cur = cur.next;
            lists[minIndex] = lists[minIndex].next;
        }
        return dummyHead.next;
    }

}
